package com.qianbao.qianbaobusiness.ui.cashier;

import android.annotation.SuppressLint;

import com.androidbase.base.mvp.RxMvpPresenter;
import com.androidbase.utils.AndroidUtils;
import com.androidbase.utils.RxUtils;
import com.androidbase.utils.ToastUtils;
import com.baseandroid.config.BossApi;
import com.baseandroid.config.MyApi;
import com.baseandroid.retrofit.RxObserver;
import com.baseandroid.retrofit.json.Data;
import com.baseandroid.utils.DESedeEncrypt;
import com.baseandroid.utils.JsonUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.qianbao.qianbaobusiness.R;
import com.qianbao.qianbaobusiness.json.BalanceAmount2Bean;
import com.qianbao.qianbaobusiness.json.BossRequestBody;
import com.qianbao.qianbaobusiness.json.EmsStatus;
import com.qianbao.qianbaobusiness.json.RequestBean;

import java.util.HashMap;
import java.util.List;

import javax.inject.Inject;

import timber.log.Timber;

public class HistoryBalancePresenter extends RxMvpPresenter<HistoryBalanceView> {
    BossApi bossApi;
    MyApi myApi;

    @Inject
    public HistoryBalancePresenter(MyApi myApi, BossApi api) {
        bossApi = api;
        this.myApi = myApi;
    }

    /**
     * 根据时间段请求历史账单
     * @param posMerchantNo
     * @param defaultPageNum
     * @param defaultPageSize
     * @param mStartDate
     * @param mEndDate
     */
    public void queryDaysCheckedPage(String posMerchantNo, int defaultPageNum, int defaultPageSize, String mStartDate, String mEndDate) {
        {
            RequestBean requestBean = new RequestBean(posMerchantNo, defaultPageNum, defaultPageSize, mStartDate, mEndDate);
            requestBean.setmCode(posMerchantNo);
            requestBean.setPageNum(defaultPageNum);
            requestBean.setPageSize(defaultPageSize);
            requestBean.setStartTime(mStartDate);
            requestBean.setEndTime(mEndDate);
            String s = JsonUtil.toJson(requestBean);
            Timber.e("request " + s);
            try {
                s = DESedeEncrypt.encrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I", s);
            } catch (Exception e) {
                e.printStackTrace();
            }
            HashMap<String, String> paraMap = new HashMap<>();
            paraMap.put("context", s);
            BossRequestBody body = new BossRequestBody(s);
            bossApi.queryDaysCheckedPage(body)
                    .compose(RxUtils.applySchedulersLifeCycle(getView()))
                    .subscribe(new RxObserver<Data<String>>() {

                        @SuppressLint("LongLogTag")
                        @Override
                        public void onNext(Data<String> stringData) {
                            if (RxObserver.checkJsonCode(stringData, true)) {
                                try {
                                    String result = DESedeEncrypt.decrypt("zm39nfhzKZ7EC4AskZSM1UBtDtlXJZ0I", stringData
                                            .getResult()
                                            .toString());
                                    Timber.e("result = " + result);
                                    List<BalanceAmount2Bean> bean = new Gson().fromJson(result, new TypeToken<List<BalanceAmount2Bean>>() {
                                    }.getType());
                                    getView().onSuccess(bean);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        }

                        @Override
                        public void onError(Throwable e) {
                            getView().onError(e);
                        }
                    });
        }
    }

    /**
     * 发送历史账单
     * @param historyType
     * @param date
     * @param date1
     * @param merchantNos
     * @param userNo
     * @param email
     */
    public void sendEmail(String historyType, String date, String date1, String merchantNos, String userNo, String email) {
        myApi.sendEmail(historyType, date, date1, merchantNos, userNo, email)
                .compose(RxUtils.applySchedulersLifeCycle(getView()))
                .subscribe(new RxObserver<Data<EmsStatus>>() {
                    @Override
                    public void onComplete() {
                        //                        getView().onComplete();
                    }

                    @Override
                    public void onNext(Data<EmsStatus> bean) {
                        if (RxObserver.checkJsonCode(bean, false)) {
                            ToastUtils.showLongToast(AndroidUtils.getContext()
                                    .getResources()
                                    .getString(R.string.send_success));
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        Timber.e("1111");
                        //                        getView().error();
                    }
                });
    }
}
